import os

base_path = os.getcwd().split('\\StockMonitor')[0] + '\\StockMonitor\\'
stock_path = base_path.split('\\stock')[0] + '\\stock\\'

from utils import select_util, excel_util, doc_util
from common.fund_gp_dict import fund_gp_dict
from service.data_service import stock_dict


def filtrate_path(doc_path, doc_name):
    print('筛选板块关联度')
    doc_name = select_util.select_doc_name(doc_path)
    doc_info = select_util.select_by_name(doc_path, doc_name)
    gen_result_excel(doc_info, doc_name)


def filtrate():
    print('筛选板块关联度')
    doc_path = stock_path + '\\plate\\self\\'
    doc_name = select_util.select_doc_name(doc_path)
    if doc_name == '' or doc_name == '00':
        # 重新生成
        print('重新生成')
        doc_name = gen_plate(doc_path)
    doc_info = select_util.select_by_name(doc_path, doc_name)
    gen_result_excel(doc_info, doc_name)


# 生成新的板块
def gen_plate(path):
    file_names = {}
    for root, dirs, files in os.walk(path):
        for file in files:
            infos = file.split('.')
            file_names[infos[0]] = file
    while 1:
        name_w = input('请输入新的名称(例:10.军工)')
        if name_w.split('.')[0] in file_names.keys():
            print('序号重复,选择文件')
            name_w = file_names[name_w]
            break
        elif len(name_w.split('.')) != 2:
            print('格式不符合')
        else:
            break
    file_w = open(path + name_w, mode='w', encoding='utf-8')
    source_path = stock_path + '\\common\\plates\\'
    while 1:
        print(source_path)
        source_path_len = len(source_path.split('\\'))
        # 沪深板块
        file_names = doc_util.get_file_path_names(source_path)
        if len(file_names) == 1:
            if os.path.exists(source_path):
                source_path = file_names[0]
                break
            else:
                source_path = stock_path + '\\common\\plates\\'
                print('路径错误重新输入')
                continue
        path_dict = {}
        for file_path_name in file_names:
            if len(file_path_name.split('\\')) >= (source_path_len + 1):
                path_dict[file_path_name.split('\\')[source_path_len - 1]] = 1
        for file_path in path_dict:
            print(file_path)
        print(source_path)
        source_path_add = input('*******请输入增加的路径:')
        for file_path_name in path_dict:
            if file_path_name.__contains__(source_path_add):
                source_path += (file_path_name + '\\')
                break
    file_r = open(source_path, mode='r', encoding='utf-8')
    info_r = doc_util.get_doc_info(file_r)
    for info in info_r:
        code = eval(info)['f12']
        if code not in stock_dict:
            continue
        file_w.write(str(stock_dict[code]) + '\n')
    file_r.close()
    file_w.close()
    return name_w


def gen_result_excel(doc_info, doc_name):
    result = {}
    for key in fund_gp_dict:
        full_ratio = 0
        path = stock_path + '\\common\\funds\\detail\\' + key
        if os.path.exists(path):
            fund_file = open(path, mode='r', encoding='utf-8')
            while 1:
                one = fund_file.readline()
                if not one:
                    break
                one = one[:-1]
                if one == '':
                    break
                stock_info = eval(one)
                if 'code' not in stock_info:
                    print(stock_info)
                    continue
                code = stock_info['code']
                if code not in stock_dict:
                    continue
                ratio = float(stock_info['ratio'].split('%')[0])
                if code in doc_info:
                    full_ratio += ratio
            fund_file.close()
        if full_ratio > 8:
            result[key] = full_ratio
    content_arr = [['编码', '名称', '类型', '总占比']]
    for key in result:
        info = []
        info.append(key)
        info.append(fund_gp_dict[key]['name'])
        info.append(fund_gp_dict[key]['type'])
        info.append(result[key])
        content_arr.append(info)
    excel_path = stock_path + '\\fund\\result\\'
    excel_util.gen_an_excel(excel_path, doc_name, '', content_arr)


if __name__ == '__main__':
    print('start')
    filtrate()
    print('end')
